package com.wzw.springboot.principle.use.function;

import com.wzw.springboot.principle.use.batch.simple.Person;
import lombok.extern.slf4j.Slf4j;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Wangzhiwen
 */
@Slf4j
public class LambdaUtils {

    private LambdaUtils() {

    }

    public static <T> SerializedLambda resolve(SFunction<T, ?> func) {
        try {
            Method writeReplace = func.getClass().getDeclaredMethod("writeReplace");
            writeReplace.setAccessible(true);
            Object invoke = writeReplace.invoke(func);
            return ((SerializedLambda) invoke);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException("Get SerializedLambda Error", e);
        }
    }

    public static void main(String[] args) {
        SerializedLambda resolve = resolve(Person::getName);
        System.out.println(resolve.getImplMethodName());
        System.out.println(resolve.getImplClass());
        System.out.println(resolve);
    }

}
